/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.servlet.crawl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.tkorg.coauthorgraph.bo.AuthorPaperBO;
import uit.tkorg.coauthorgraph.utility.CommonHTMLProcessor;
import uit.tkorg.coauthorgraph.utility.MASFetcher;
import uit.tkorg.coauthorgraph.utility.MASResultAnalyze;

/**
 * @author Tin Huynh
 *
 */
public class MASManualCrawlingServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            PrintWriter out = response.getWriter();
            String authorName = request.getParameter("authorName");
            MASFetcher fetcher = new MASFetcher();
            String _pageContent = fetcher.fetchHTMLContentForAuthor(authorName);
            CommonHTMLProcessor htmlParser = new CommonHTMLProcessor();
            AuthorPaperBO authorPaperBO = new AuthorPaperBO();
            double existedTotalPaperOfAnAuthor = authorPaperBO.countTotalPaperOfAuthor(authorName);
            ArrayList publicationList =  MASResultAnalyze.getPublications(_pageContent, authorName, existedTotalPaperOfAnAuthor);
            HttpSession session = request.getSession();
            session.setAttribute("publicationList", publicationList);
            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/MASManualCrawling.jsp");
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
